package com.liesheng.haylou.base;

import androidx.fragment.app.Fragment;

import com.liesheng.haylou.net.HttpCallback;
import com.liesheng.haylou.net.HttpService;
import com.liesheng.haylou.net.RetrofitFactory;

import java.util.ArrayList;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Date:2019/11/21
 * <p>
 * Author:wuzhiguang
 * <p>
 * Description:
 */
public class BaseFunFragment extends Fragment implements IBaseFun {

    ArrayList<Subscription> subscriptions = new ArrayList<>();

    @Override
    public void showToast(String msg) {

    }

    @Override
    public void showToast(int strId) {

    }


    private PermissionListener mlistener;

    /**
     * 权限申请
     *
     * @param permissions 待申请的权限集合
     * @param listener    申请结果监听事件
     */
    @Override
    public void requestRunTimePermission(String[] permissions, PermissionListener listener) {

        ((BaseFunActivity) getActivity()).requestRunTimePermission(permissions, listener);
    }


    @Override
    public void showLoadingDialog() {
        ((BaseFunActivity) getActivity()).showLoadingDialog();
    }

    @Override
    public void showLoadingDialog(String msg) {
        ((BaseFunActivity) getActivity()).showLoadingDialog();
    }

    @Override
    public void dismissLoadingDialog() {
        ((BaseFunActivity) getActivity()).dismissLoadingDialog();
    }

    @Override
    public void addSubscription(Subscription subscription) {
        if (subscription != null) {
            subscriptions.add(subscription);
        }
    }

    @Override
    public void unsubscribe() {
        for (Subscription subscription : subscriptions) {
            if (subscription != null && !subscription.isUnsubscribed())
                subscription.unsubscribe();
        }
    }

    @Override
    public <T extends BaseResult> void requestHttp(Observable<T> observable, final HttpCallback<T> callback) {

        ((BaseFunActivity) getActivity()).requestHttp(observable, callback);
//        Subscription subscription = observable.subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<T>() {
//                    @Override
//                    public void onCompleted() {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        e.printStackTrace();
//                    }
//
//                    @Override
//                    public void onNext(T t) {
//                        if (callback != null)
//                            callback.onNext(t);
//                    }
//                });
//
//        addSubscription(subscription);
    }

    @Override
    public HttpService buildHttpService() {
        return RetrofitFactory.getHttpService();
    }

    public String getStr(int rid){
        return getResources().getString(rid);
    }
    public String getStr(int rid,Object... formatArgs){
        return  getResources().getString(rid,formatArgs);
    }
}
